package com.kuhyygy.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.kuhyygy.R;
import com.kuhyygy.adapter.MsgAdapter;
import com.kuhyygy.bean.Msg;

public class ChatActivity extends Activity {
	
	private List<Msg> chats = new ArrayList<Msg>();
	
	private ListView listView;
	private EditText ed;
	private Button btn;
	private MsgAdapter msgAdapter;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chatwin);
		initMsg();
		msgAdapter = new MsgAdapter(ChatActivity.this, R.layout.chatitem, chats);
		listView = (ListView) findViewById(R.id.chatMsgView);
		ed = (EditText) findViewById(R.id.msgText);
		btn = (Button) findViewById(R.id.sendBtn);
		listView.setAdapter(msgAdapter);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Random r = new Random();
				Msg m = new Msg(ed.getText().toString(), (r.nextInt()%2==0)? Msg.TYPE_SEND: Msg.TYPE_RECEIVED);
				chats.add(m);
				msgAdapter.notifyDataSetChanged();
				listView.setSelection(chats.size());
				ed.setText("");
			}
		});
	}

	private void initMsg() {
		Msg msg1 = new Msg("hey guy", Msg.TYPE_RECEIVED);
		chats.add(msg1);
		Msg msg2 = new Msg("hey guy, how's your day today", Msg.TYPE_SEND);
		chats.add(msg2);
		Msg msg3 = new Msg("nice", Msg.TYPE_RECEIVED);
		chats.add(msg3);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chat, menu);
		return true;
	}

}
